﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Lenovo.CFI.BLL;
using Lenovo.CFI.Common;
using Lenovo.CFI.Web.Helper;
using System.Web.UI.HtmlControls;
using Lenovo.CFI.WebControls;
using Lenovo.CFI.BLL.DicMgr;

namespace Lenovo.CFI.Web.VP
{
    public partial class TopicSetDcIP : System.Web.UI.UserControl
    {
        TopicBl topicBl = new TopicBl();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                int? id = UrlHelper.GetQueryStringID();
                Topic topic = null;
                if (id.HasValue)
                {
                    topic = topicBl.GetTopicByID(id.Value);
                }
                if (topic == null) ErrorHelper.RaiseNoValueError();

                ViewState["topicid"] = topic.ID;

                List<TopicDcIp> dcOwners = new List<TopicDcIp>();
                dcOwners = topicBl.GetDCAndIPOwner(topic.ID);
                TopicDcIp ipOwner = dcOwners.Find(di=>di.IsIP);
                if (ipOwner != null) dcOwners.Remove(ipOwner);

                this.RepDcAndIp.DataSource = dcOwners;
                this.RepDcAndIp.DataBind();
                if (ipOwner != null) this.OuacIpOwner.Text = ipOwner.User;


                if (topic.SuggestDate.HasValue)
                    this.DpSuggestion.Value = topic.SuggestDate.Value.ToString("yyyy-MM-dd");
                else
                    this.DpSuggestion.Value = "";

                if (topic.MaterialDate.HasValue)
                    this.DpSubmitMaterial.Value = topic.MaterialDate.Value.ToString("yyyy-MM-dd");
                else
                    this.DpSubmitMaterial.Value = "";

                if (topic.ReportDate.HasValue)
                    this.DpGmoReport.Value = topic.ReportDate.Value.ToString("yyyy-MM-dd");
                else
                    this.DpGmoReport.Value = "";

                if (topic.MktDate.HasValue)
                    this.DpMktReport.Value = topic.MktDate.Value.ToString("yyyy-MM-dd");
                else
                    this.DpMktReport.Value = "";

                if (!String.IsNullOrEmpty(topic.Principal))
                    this.TxtPrincipal.Text = topic.Principal;
                else
                    this.TxtPrincipal.Text = "liuyh(刘宇红)";

            }
        }


        protected void RepDcAndIp_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                TopicDcIp dc = (TopicDcIp)e.Item.DataItem;
                ((Label)e.Item.FindControl("LblDcOwner")).Text = IdeaTypeDicMgr.GetByCode(dc.Aspect).Title;
                ((HtmlInputHidden)e.Item.FindControl("HiDcType")).Value = dc.Aspect;
                ((OneUserAutoComplete)e.Item.FindControl("OuacDcOwner")).Text = dc.User;
            }
        }

        protected void BtnSave_Click(object sender, EventArgs e)
        {
            int topicID = Convert.ToInt32(ViewState["topicid"]);

            Dictionary<string, string> dcUsers = new Dictionary<string, string>();
            foreach (RepeaterItem row in RepDcAndIp.Items)
            {
                if (row.ItemType == ListItemType.Item || row.ItemType == ListItemType.AlternatingItem)
                {
                    string code = (((HtmlInputHidden)row.FindControl("HiDcType"))).Value;
                    dcUsers.Add(code, ((OneUserAutoComplete)row.FindControl("OuacDcOwner")).Text);
                }
            }
            string ipUser = this.OuacIpOwner.Text;

            //topic.SuggestDate = this.DpSuggestion.Date;
            //topic.MaterialDate = this.DpSubmitMaterial.Date;
            //topic.ReportDate = this.DpGmoReport.Date;
            //topic.MktDate = this.DpMktReport.Date;
            //topic.Principal = this.TxtPrincipal.Text;

            try
            {
                this.topicBl.EditTopicDcAndIp(topicID, dcUsers, ipUser);
                
                this.topicBl.EditTopic(topicID, this.DpSuggestion.Date.Value,
                    this.DpSubmitMaterial.Date.Value, this.DpGmoReport.Date.Value,
                    this.DpMktReport.Date.Value, this.TxtPrincipal.Text);

                MessageHelper.Show("Success!", this.Page);
            }
            catch (ApplicationException aex)
            {
                MessageHelper.Show(aex.Message, this.Page);
            }
            catch (Exception ex)            // Ajax无法使用默认异常处理程序处理异常
            {
                ErrorHelper.Handle(ex);
            }
        }

        protected void BtnFinish_Click(object sender, EventArgs e)
        {
            int topicID = Convert.ToInt32(ViewState["topicid"]);

            try
            {
                //topicBl.StartSuggestion(topicID);

                //MessageHelper.Show("Success!", this.Page);

                System.Web.UI.ScriptManager.RegisterStartupScript(this.BtnFinish, this.Page.GetType(), "Close",
                    String.Format(@"setTimeout(""window.location='{0}';"", 1000);", UrlHelper.GenerateUrl("topiclist")), true);

            }
            catch (ApplicationException ex)
            {
                MessageHelper.Show(ex.Message, this.Page);
            }
        }
    }
}